import apiBinderFactory from '@/utils/apiBinderFactory';
import filterValidKeyFactory from '@/utils/filterValidKeyFactory';

const prefix = 'user';

async function register({ username, password, email }) {
  return await this.$axios.$post(`/${prefix}`, {
    username,
    password,
    email
  });
}

async function login({ loginName, password }) {
  return await this.$axios.$post(`/${prefix}/login`, {
    loginName,
    password
  });
}

async function logout() {
  return await this.$axios.$post(`/${prefix}/logout`);
}

async function getInfo() {
  return await this.$axios.$get(`/${prefix}`);
}

const filterValidKeyOfUniqueCondition = filterValidKeyFactory(['username', 'email']);
async function unique(data) {
  return await this.$axios.$post(`/${prefix}/unique`, filterValidKeyOfUniqueCondition(data));
}

async function del() {
  return await this.$axios.$delete(`/${prefix}`);
}

export default apiBinderFactory({
  register,
  login,
  logout,
  getInfo,
  unique,
  del
});
